<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="1"
      formType="add"
      labelWidth="100"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton v-if="type !== 'show'" :click="saveDialog" type="primary">保存</AjaxButton>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { inOrUpDissatisfiedCause } from '@/api/customerCare/visitRules/dissatisfaction';
import { systemCode,testText16 } from '@/utils/validate';
export default {
  name: 'dissatisfactionDetail',
  mixins: [dictionary],
  components: {},
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList(){
      return [
        {
          type: 'INPUT',
          label: '不满意原因',
          fieldName: 'baseDataName',
          placeholder: '不满意原因',
          rules: [{ required: true, validator:testText16, trigger: 'blur' }],
          initialValue: ''
        }
      ]
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    closeDialog(type) {
      this.$emit('close', false, type);
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      const name = this.formList.find(item=>item.fieldName === 'baseDataName').initialValue;
      if(!name||name==''){
        this.$message('请输入名称');
        return;
      }else{
        var g = /^[\u4E00-\u9FA5]{0,16}$/;
        if (!g.test(name)) {
          this.$message('纯文本内容，最多16字符');
          return;
        }
      }

      if (this.saveData !== null) {
        let res = {};
        this.saveData.baseDataType = 1001;
        if (this.type === 'edit') {
          this.saveData.baseDataId = this.rowData.baseDataId;
        }
        res = await inOrUpDissatisfiedCause(this.saveData);

        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }else{
          this.$message(`${res.errMsg}`);
        }
      }
    }
  }
}
</script>
